package com.cheyun.netsalev3.viewmodel

import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.bean.dm.DmDealUser
import com.cheyun.netsalev3.repository.DmClueTransferActivityRepository
import com.cheyun.netsalev3.widget.MyDialog
import com.jeremyliao.liveeventbus.LiveEventBus
import java.util.*

class DmClueTransferActivityViewModel(var clueTransferActivityRepository: DmClueTransferActivityRepository) : BaseViewModel() {
    //移交用户
    var member:MutableList<DmDealUser> = mutableListOf();
    var memberName = ObservableField<String>("")

    var memberPosition = -1

    init {
        getDealUser();
    }

    fun getDealUser(){
        var method: (List<DmDealUser>) -> Unit = {
            member.addAll(it)
        }
        clueTransferActivityRepository.getDealusers(method);
    }
    /**
     * 移交
     */
    fun yijiao(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            member?.apply {
                var list = ArrayList<DialogParam>()
                for (it in this) {
                    var item = DialogParam(it.name, it.userId.toString())
                    list.add(item)
                }
                var diolog = MyDialog.newInstance("移交线索", list)
                diolog.huidiaPosition = this@DmClueTransferActivityViewModel::myPopSetData
                diolog.show(content.supportFragmentManager, "missiles2")
            }
        }
    }

    /**
     * 选中移交回调
     */
    fun myPopSetData(position: Int) {
        memberPosition = position
        if (memberPosition != -1) {
            memberName.set(member!![memberPosition].name)
        }
    }

    /**
     * 完成
     */
    fun clickBut(view: View) {
        if (memberPosition == -1) {
            return
        }

        var toUserId = ""
        var toUser = ""
        toUserId = member?.get(memberPosition)?.userId.toString()
        toUser = member?.get(memberPosition)?.name

        var method: () -> Unit = {
            setData(view)
        }

        clueTransferActivityRepository.clueTransfer(toUserId,toUser,"", method)
    }

    /**
     * 成功回调
     */
    fun setData(view: View) {
        LiveEventBus.get().with("dmclueupdatge").postValue("")
        myFinish();
        MyToast("分配成功")
    }

}